//
//  XMSendInviteMsgCell.m
//  XunMi
//
//  Created by MacBook on 2024/4/22.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMSendInviteMsgCell.h"
#import "XMInviteMessage.h"
#import <RongIMLib/RongIMLib.h>

#define kSendInviteMsgContentViewH 110
#define kReceiveInviteMsgContentViewH 145

#define kInviteImgW 58
#define kInviteImgSpace 12
#define kLabelSpace 15


@interface XMSendInviteMsgCell ()
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *sendLa;
@property (nonatomic, strong) UILabel *nameNumLa;
@property (nonatomic, strong) UIView *sendView;
@property (nonatomic, strong) UILabel *toHerLa;
@property (nonatomic, strong) UIButton *goldBtn;

@end


@implementation XMSendInviteMsgCell

+ (CGSize)sizeForMessageModel:(RCMessageModel *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight {
    
    if (model.messageDirection == MessageDirection_RECEIVE) {
        return CGSizeMake(collectionViewWidth, kReceiveInviteMsgContentViewH + extraHeight);
    }
    return CGSizeMake(collectionViewWidth, kSendInviteMsgContentViewH + extraHeight);
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setDataModel:(RCMessageModel *)model {
    [super setDataModel:model];
    XMInviteMessage *message = (XMInviteMessage *)model.content;
    NSString *nameNumStr = message.msgType.intValue == 1 ? kStringFormat(@"%@ x%@", message.name, message.number) : message.name;
    self.nameNumLa.text = nameNumStr;
    CGFloat nameNumW = ceil([nameNumStr widthForFont:kUIFont(14)]) + 1.f;
    NSString *descStr = @"";
    CGFloat descW = 0.f;
    [self.goldBtn setTitle:kStringFormat(@"%@金币", message.golds) forState:UIControlStateNormal];
    NSString *toHerText = @"";
    NSString *hasToHerText = @"";
    switch (message.msgType.intValue) {
        case 1:
            descStr = model.messageDirection == MessageDirection_RECEIVE ? @"对方希望您送她" : @"已邀请对方送您";
            [self.imgView sd_setImageWithURL:kStringToUrl(message.imgUrl) placeholderImage:kDefaultCoverImg];
            toHerText = @"送给她";
            hasToHerText = @"已送出";
            break;
        case 2:
            descStr = model.messageDirection == MessageDirection_RECEIVE ? @"对方希望为她" : @"已邀请她为您";
            [self.imgView setImage:kGetImage(@"con_invite_guard")];
            toHerText = @"开启守护";
            hasToHerText = @"已守护";
            break;
        case 3:
            descStr = model.messageDirection == MessageDirection_RECEIVE ? @"邀请您试试手气" : @"邀请他试试手气";
            [self.imgView setImage:kGetImage(@"con_invite_box")];
            toHerText = @"开盲盒";
            hasToHerText = @"已开启";
            break;
        default:
            break;
    }
    descW = ceil([descStr widthForFont:kUIFont(14)]) + 1;
    
    CGFloat contentW = kLabelSpace + MAX(nameNumW, descW) + kInviteImgSpace + kInviteImgW + kInviteImgSpace;
    
    self.messageContentView.contentSize = CGSizeMake(contentW, model.messageDirection == MessageDirection_RECEIVE ? kReceiveInviteMsgContentViewH : kSendInviteMsgContentViewH);
    
    if (model.messageDirection == MessageDirection_RECEIVE) {
        self.bubbleBackgroundView.image = kGetImage(@"con_invite_rebg");
        self.sendLa.text = descStr;
        self.sendLa.textColor = k1F1F1F;
        self.nameNumLa.textColor = k1F1F1F;
        
        self.imgView.frame = kFRAME(contentW - kInviteImgSpace - kInviteImgW, 30, kInviteImgW, kInviteImgW);
        
        self.sendLa.frame = kFRAME(kLabelSpace, 34, descW, 20);
        
        self.nameNumLa.frame = kFRAME(kLabelSpace, self.sendLa.gk_y + 28, nameNumW, 20);
        self.sendView.hidden = NO;
        
    } else {
        self.bubbleBackgroundView.image = kGetImage(@"con_invite_sendbg");
        self.sendLa.text = descStr;
        self.sendLa.textColor = UIColor.whiteColor;
        self.nameNumLa.textColor = UIColor.whiteColor;
        
        self.imgView.frame = kFRAME(kInviteImgSpace, 36, kInviteImgW, kInviteImgW);
        
        self.sendLa.frame = kFRAME(kInviteImgSpace * 2 + kInviteImgW, 40, descW, 20);
        
        self.nameNumLa.frame = kFRAME(kInviteImgSpace * 2 + kInviteImgW, kSendInviteMsgContentViewH - 40, nameNumW, 20);
        self.sendView.hidden = YES;
    }
    
    RCMessage *msg = [[RCIMClient sharedRCIMClient] getMessage:model.messageId];
    NSLog(@"%@ %@", msg.expansionDic, [msg.expansionDic toString:@"status"]);
    if ([msg.expansionDic objectForKey:@"status"]) {
        NSString *status = [msg.expansionDic toString:@"status"];
        _sendView.backgroundColor = status.intValue == 0 ? kHEX(@"#FD5067") : kHEX(@"#C8C8C8");
        _sendView.userInteractionEnabled = status.intValue == 0;
        self.goldBtn.hidden = status.intValue == 1;
        if (status.intValue == 1) {
            [self.toHerLa mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.sendView);
            }];
            self.toHerLa.text = hasToHerText;
        } else {
            [self.toHerLa mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.sendView).offset(3);
                make.right.equalTo(self.sendView).offset(-3);
                make.top.equalTo(self.sendView).offset(3);
            }];
            self.toHerLa.text = toHerText;
        }
    } else {
        [self.toHerLa mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.sendView).offset(3);
            make.right.equalTo(self.sendView).offset(-3);
            make.top.equalTo(self.sendView).offset(3);
        }];
        self.toHerLa.text = toHerText;
    }
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    [self showBubbleBackgroundView:YES];
    [self.messageContentView addSubview:self.imgView];
    [self.messageContentView addSubview:self.sendLa];
    [self.messageContentView addSubview:self.nameNumLa];
    [self.messageContentView addSubview:self.sendView];
    [self.sendView addSubview:self.toHerLa];
    [self.sendView addSubview:self.goldBtn];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.sendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(38);
        make.left.equalTo(self.messageContentView).offset(14);
        make.right.equalTo(self.messageContentView).offset(-14);
        make.bottom.equalTo(self.messageContentView).offset(-14);
    }];
    
    [self.toHerLa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sendView).offset(3);
        make.right.equalTo(self.sendView).offset(-3);
        make.top.equalTo(self.sendView).offset(3);
    }];
    
    [self.goldBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sendView).offset(3);
        make.right.equalTo(self.sendView).offset(-3);
        make.bottom.equalTo(self.sendView).offset(-4);
    }];
}

- (void)tap {
    if (self.sendBlock) {
        self.sendBlock(self.model);
    }
}

#pragma mark - Getter
- (UIImageView *)imgView {
    if (_imgView == nil) {
        _imgView = [[UIImageView alloc] init];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imgView;
}

- (UILabel *)sendLa {
    if (_sendLa == nil) {
        _sendLa = [[UILabel alloc] init];
        _sendLa.font = kUIFont(14);
        _sendLa.textAlignment = NSTextAlignmentLeft;
    }
    return _sendLa;
}

- (UILabel *)nameNumLa {
    if (_nameNumLa == nil) {
        _nameNumLa = [[UILabel alloc] init];
        _nameNumLa.font = kUIFont(14);
        _nameNumLa.textAlignment = NSTextAlignmentLeft;
    }
    return _nameNumLa;
}
- (UIButton *)goldBtn {
    if (_goldBtn == nil) {
        _goldBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _goldBtn.adjustsImageWhenHighlighted = NO;
        _goldBtn.userInteractionEnabled = NO;
        [_goldBtn setImage:kGetImage(@"con_invite_gold") forState:UIControlStateNormal];
        [_goldBtn setImagePosition:(XMImagePositionLeft) spacing:4];
        _goldBtn.titleLabel.font = kUIFont(10);
    }
    return _goldBtn;
}

- (UIView *)sendView {
    if (_sendView == nil) {
        _sendView = [[UIView alloc] initWithFrame:CGRectZero];
        _sendView.hidden = YES;
        _sendView.backgroundColor = kHEX(@"#FD5067");
        [_sendView radiusWithRadius:19 corner:UIRectCornerAllCorners];
        _sendView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [_sendView addGestureRecognizer:tap];
    }
    return _sendView;
}

- (UILabel *)toHerLa {
    if (_toHerLa == nil) {
        _toHerLa = [[UILabel alloc] init];
        _toHerLa.text = @"送给他";
        _toHerLa.font = kUIFont(14);
        _toHerLa.textColor = UIColor.whiteColor;
        _toHerLa.textAlignment = NSTextAlignmentCenter;
    }
    return _toHerLa;
}

@end
